package main

import (
	"wzry/mysql"

	"github.com/gin-contrib/multitemplate"
	"github.com/gin-gonic/gin"
)

func index(ctx *gin.Context) {
	ctx.HTML(200, "index.html", nil)
}

func hero(ctx *gin.Context) {
	type hero struct {
		Name string
		Job  string
		Hid  int
	}
	heros := []hero{
		{Name: "澜", Job: "刺客", Hid: 1},
		{Name: "镜", Job: "刺客", Hid: 2},
		{Name: "曜", Job: "战士", Hid: 3},
		{Name: "瑶", Job: "辅助", Hid: 4},
	}
	ctx.HTML(200, "hero.html", gin.H{
		"heros": heros,
	})
}
func skin(ctx *gin.Context) {
	ctx.HTML(200, "skin.html", nil)
}
func zhuangbei(ctx *gin.Context) {
	ctx.HTML(200, "zhuangbei.html", nil)
}
func createMyRender() multitemplate.Renderer {
	r := multitemplate.NewRenderer()
	r.AddFromFiles("index.html", "html/base.html", "html/index.html")
	r.AddFromFiles("hero.html", "html/base.html", "html/hero.html")
	r.AddFromFiles("zhuangbei.html", "html/base.html", "html/zhuangbei.html")
	r.AddFromFiles("skin.html", "html/base.html", "html/skin.html")
	r.AddFromFiles("heroadd.html", "html/base.html", "html/heroadd.html")

	return r
}

func Heroadd(ctx *gin.Context) {
	name := ctx.PostForm("name")
	job := ctx.PostForm("job")
	skill1 := ctx.PostForm("skill1")
	skill2 := ctx.PostForm("skill2")
	skill3 := ctx.PostForm("skill3")
	mingwen := ctx.PostForm("mingwen")
	h1 := mysql.Hero{
		BaseModel: mysql.BaseModel{Name: name},
		Job:       job,
		Skill1:    skill1,
		Skill2:    skill2,
		Skill3:    skill3,
		MingWen:   mingwen,
	}
	DB.Create(&h1)
	ctx.JSON(200, gin.H{
		"json": h1,
	})
}

func HeroAddHtml(ctx *gin.Context) {
	ctx.HTML(200, "heroadd.html", nil)
}

var DB = mysql.MysqlInit()

func main() {
	// 初始化mysql

	r := gin.Default()

	// r.LoadHTMLGlob("html/*")

	r.HTMLRender = createMyRender()

	// 模版语法的变量渲染
	r.GET("/", index)
	r.GET("/hero", hero)
	r.GET("/skin", skin)
	r.GET("/zhuangbei", zhuangbei)

	r.GET("/hero/add", HeroAddHtml)
	r.POST("/hero/add", Heroadd)
	r.Run(":8000")
}
